<?php

/**
 * 后台操作日志模型
 *
 * @author JiangJian <silverd@sohu.com>
 * @copyright 2011-2012 xiangle.com
 * $Id: OpLog.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @version    2012-02-14  ::  JiangJian  ::  Create File
 */
class Model_System_OpLog extends Model_AbstractDao
{

    protected $_dbName     = 'BingoMobi';
    protected $_tableName  = 'bg_sys_op_log';
    protected $_primaryKey = 'id';
    protected $_nameField  = '';

    /**
     * 操作类型
     *
     * @var array
     */
    private $_opTypes = array(
        'UP_CITY'         => '修改城市',
        'ADD_CITY'        => '添加城市',
        'UP_QUDAO'        => '修改渠道',
        'ADD_QUDAO'       => '添加渠道',
        'UP_LOCATION'     => '修改点位',
        'ADD_LOCATION'    => '添加点位',
        'START_LOCATION'  => '启用点位',
        'FORBID_LOCATION' => '冻结点位',
        'UP_GIFT'         => '修改礼品',
        'ADD_GIFT'        => '添加礼品',
        'START_GIFT'      => '启用礼品',
        'FORBID_GIFT'     => '冻结礼品',
        'UP_USER'         => '修改用户',
        'ADD_USER'        => '添加用户',
        'START_USER'      => '启用用户',
        'FORBID_USER'     => '冻结用户',
        'DEL_USER'        => '删除用户',
    );

    public function getOpTypes()
    {
        return $this->_opTypes;
    }

    /**
     * 添加一条日志
     *
     * @param string $type
     * @param string $content
     * @param array $authUser 操作人信息
     * @return bool
     */
    public function add($type, $content, $authUser = array())
    {
        if (!$authUser) {
            $authUser['admin_id']   = -1;
            $authUser['admin_name'] = '系统';
        }

        $type = trim($type);
        if (!$type || !isset($this->_opTypes[$type])) {
            return '操作类型未定义';
        }

        $content = trim($content);
        if (!$content) {
            return '操作内容不能为空';
        }
        $setArr = array(
            'type'        => $type,
            'content'     => $content,
            'admin_id'    => $authUser['admin_id'],
            'admin_name'  => $authUser['admin_name'],
            'role_name'   => $authUser['role_name'],
            'city_id'     => $authUser['city_id'],
            'create_time' => time(),
        );

        $insertId = $this->_db()->insert('bg_sys_op_log', $setArr);
        if ($insertId < 1) {
            return '操作日志入库失败，请联系系统管理员';
        }

        return $insertId;
    }

}